package com.example.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.BaseContext;
import com.example.common.R;
import com.example.domain.OrderDetail;
import com.example.domain.Orders;
import com.example.domain.ShoppingCart;
import com.example.service.OrderDetailService;
import com.example.service.OrdersService;
import com.example.service.ShoppingCartService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/order")
public class OrdersController {

    @Autowired
    private OrderDetailService orderDetailService;

    @Autowired
    private ShoppingCartService shoppingCartService;


    @Autowired
    private OrdersService ordersService;

    @Autowired
    private RedisTemplate redisTemplate;

    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders) {

        ordersService.submit(orders);
        redisTemplate.delete("shoppingCart");
        return R.success("下单成功！");
    }

    @GetMapping("/userPage")
    public R<Page> userPage(int page, int pageSize) {

        Page pg = new Page(page, pageSize);

        LambdaQueryWrapper<Orders> wrapper = new LambdaQueryWrapper<Orders>();

        wrapper.eq(Orders::getUserId, BaseContext.getThreadLocal());

        wrapper.orderByDesc(Orders::getOrderTime);

        ordersService.page(pg, wrapper);

        return R.success(pg);


    }

    @GetMapping("/page")
    public R<Page> pages(int page, int pageSize, Long number, String beginTime, String endTime) {

        Page pg = new Page(page, pageSize);

        LambdaQueryWrapper<Orders> wrapper = new LambdaQueryWrapper<Orders>();

        wrapper.like(number != null, Orders::getId, number);

        wrapper.between(beginTime != null && endTime != null, Orders::getOrderTime, beginTime, endTime);

        wrapper.orderByDesc(Orders::getOrderTime);

        ordersService.page(pg, wrapper);

        return R.success(pg);

    }

    @PutMapping
    public R<String> send(@RequestBody Orders orders) {


        Long id = orders.getId();

        LambdaUpdateWrapper<Orders> wrapper = new LambdaUpdateWrapper<Orders>();

        wrapper.eq(Orders::getId, id);

        wrapper.set(Orders::getStatus, orders.getStatus());

        ordersService.update(wrapper);

        return R.success("派送成功！");
    }

    @PostMapping("/again")
    public R<String> again(@RequestBody Orders orders) {

        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<ShoppingCart>();

        wrapper.eq(ShoppingCart::getUserId, BaseContext.getThreadLocal());

        shoppingCartService.remove(wrapper);

        ordersService.again(orders.getId());

        return R.success("重新下单成功！");
    }
}